package com.imcoke.abchat.common.utils;

import com.imcoke.abchat.common.AppProperties;
import com.imcoke.abchat.domain.json.JsonRegister;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author ccoke
 * @Description 邮件工具
 * @Date: 9:53 2017-11-28
 */
@Component
public class EmailUtil {
    private JavaMailSender mMailSender;
    private AppProperties mAppProperties;
    @Autowired
    public EmailUtil(JavaMailSender mMailSender, AppProperties mAppProperties) {
        this.mMailSender = mMailSender;
        this.mAppProperties = mAppProperties;
    }
    /**
     * @Author ccoke
     * @Description target:目标邮件,subject:主题,html:正文
     */
    @Async
    public void sendMail(String target,String subject,String html) throws Exception {
        MimeMessage message = mMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(mAppProperties.getEmail());
        helper.setTo(target);
        helper.setSubject(subject);
        helper.setText(html,true);
        mMailSender.send(message);
    }

    //获取注册模板
    public String getRegisterTemplate(JsonRegister jsonRegister,String url) throws IOException, TemplateException {
        Map<String, Object> model = new HashMap();
        model.put("username",jsonRegister.getUsername());
        model.put("url", url);
        Configuration cfg= new Configuration(Configuration.VERSION_2_3_23);
        // 设定去哪里读取相应的ftl模板
        cfg.setClassForTemplateLoading(this.getClass(), "/templates/mail");
        // 在模板文件目录中寻找名称为name的模板文件
        Template template   = cfg.getTemplate("register.ftl");
        return FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
    }
    //获取忘记密码模板
    public String getForgetTemplate(String username,String url) throws IOException, TemplateException {
        Map<String, Object> model = new HashMap();
        model.put("username",username);
        model.put("url", url);
        Configuration cfg= new Configuration(Configuration.VERSION_2_3_23);
        // 设定去哪里读取相应的ftl模板
        cfg.setClassForTemplateLoading(this.getClass(), "/templates/mail");
        // 在模板文件目录中寻找名称为name的模板文件
        Template template   = cfg.getTemplate("forget.ftl");
        return FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
    }
    //获取重设邮件模板
    public String getResetTemplate(String username,String url) throws IOException, TemplateException {
        Map<String, Object> model = new HashMap();
        model.put("username",username);
        model.put("url", url);
        Configuration cfg= new Configuration(Configuration.VERSION_2_3_23);
        // 设定去哪里读取相应的ftl模板
        cfg.setClassForTemplateLoading(this.getClass(), "/templates/mail");
        // 在模板文件目录中寻找名称为name的模板文件
        Template template   = cfg.getTemplate("reset.ftl");
        return FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
    }

}
